﻿var Admin = {
    init: function(){
        this.createEventGrid();
        this.createDatePicker();
        this.buttonClicks();
        $("#btnUpdate").hide();
        $("#btnDelete").hide();
        $("#txtId").hide();
        
        //Get user list
        $.ajax({
            type: "get",
            url: "/users/getuserlist",
            contenttype: "application/json; charset=utf-8",
            datatype: "json",
            success: function (data) {
                var userList = data.ReturnModel;
                $.each(userList, function (val, userData) {
                    var option = document.createElement("OPTION");
                    option.text = userData.Username;
                    option.value = userData.UserId;
                    document.getElementById("selectTriggered").options.add(option);
                })

                $(function () {
                    $("select").dropkick();
                });
            }
        });
    },
    createEventGrid: function () {
        var convertJsonDate = function (dateValue) {
            var a = dateValue;
            var retrieveDate = new Date(parseInt(a.substr(6)));

            var month = retrieveDate.getMonth() + 1;
            var day = retrieveDate.getDate();
            var year = retrieveDate.getFullYear();
            var date = month + "/" + day + "/" + year;
            return date;
        }

        var eventStartDateFormatter = function (cellValue, options, rowObject) {
            if (rowObject.EventDateDTO != null) {
                var a = rowObject.EventDateDTO.EventStartDate;
                return convertJsonDate(a);
            }
        };

        var eventEndDateFormatter = function (cellValue, options, rowObject) {
            if (rowObject.EventDateDTO != null) {
                var a = rowObject.EventDateDTO.EventEndDate;
                return convertJsonDate(a);
            }
        };

        var eventSubsStartDateFormatter = function (cellValue, options, rowObject) {
            if (rowObject.EventDateDTO != null) {
                var a = rowObject.EventDateDTO.SubscribeStartDate;
                return convertJsonDate(a);
            }
        };

        var eventSubsEndDateFormatter = function (cellValue, options, rowObject) {
            if (rowObject.EventDateDTO != null) {
                var a = rowObject.EventDateDTO.SubscribeEndDate;
                return convertJsonDate(a);
            }
        };


        $("#grid").jqGrid({
            url: "/queue/GetEvents",
            postData: {},
            datatype: "json",
            mtype: "GET",
            width: 450,
            colNames: ["Id", 'Name', 'Description', 'TriggeredBy', "Room", "Private", "StartDate", "EndDate", "SubsStartDate", "SubsEndDate"],
            colModel: [
                { name: 'Id', index: 'Id', width: 250, sortable:false, hidden: true},
                { name: 'Name', index: 'Name', width: 250, sortable:false },
                { name: 'Description', index: 'Description', width: 250, sortable:false },
                { name: 'TriggeredBy', index: 'PersonInCharge', width: 250, sortable: false, hidden: true },
                { name: 'Room', index: "Room", width: 250, hidden: false },
                { name: 'Private', index: "Private", width: 250, hidden: true},
                { name: 'StartDate', index: "", formatter: eventStartDateFormatter, width: 250, hidden: true },
                { name: 'EndDate', index: "", formatter: eventEndDateFormatter, width: 250, hidden: true },
                { name: 'SubsStartDate', index: "", formatter: eventSubsStartDateFormatter, width: 250, hidden: true },
                { name: 'SubsEndDate', index: "", formatter: eventSubsEndDateFormatter, width: 250, hidden: true }
            ],
            rowNum: 20,
            rowList: [20, 40, 80],
            viewrecords: true,
            jsonReader: { repeatitems: false },
            height: "auto",
            onSelectRow: function (ids) {
                var row = $("#grid").jqGrid("getRowData", ids);
                $("#txtId").val(row.Id);
                $("#txtName").val(row.Name);
                $("#txtDescription").val(row.Description);
                $("#txtTriggered").val(row.TriggeredBy);
                $("#selLocation").val(row.Room);
                $("#selLocation").change();
                $("#selPrivate").val(row.Private);
                $("#dpStartDate").val(row.StartDate);
                $("#dpEndDate").val(row.EndDate);
                $("#dpSubsStartDate").val(row.SubsStartDate);
                $("#dpSubsEndDate").val(row.SubsEndDate);

                $("#btnUpdate").show();
                $("#btnDelete").show();
                $("#btnAdd").hide();
            }
        });
    },
    reloadGrid: function () {
        $("#grid").trigger("reloadGrid");
    },
    clearInputs: function () {
        $("#txtName").val("");
        $("#txtDescription").val("");
        $("#txtTriggered").val("");
        $("#dpStartDate").val("");
        $("#dpEndDate").val("");
        $("#dpSubsStartDate").val("");
        $("#dpSubsEndDate").val("");
        
    },
    createDatePicker: function () {
        $(function () {
            $("#dpStartDate").datepick({ dateFormat: 'mm/dd/yyyy', showTrigger: '#calImg' });
            $("#dpEndDate").datepick({ dateFormat: 'mm/dd/yyyy', showTrigger: '#calImg2' });
            $("#dpSubsStartDate").datepick({ dateFormat: 'mm/dd/yyyy', showTrigger: '#calImg2' });
            $("#dpSubsEndDate").datepick({ dateFormat: 'mm/dd/yyyy', showTrigger: '#calImg2' });
        });
    },
    buttonClicks: function () {
        $("#btnAdd").click(function () {
            var eventRecurring = new Object();
            eventRecurring.CreatedDate = new Date();

            var eventDateDTO = new Object();
            eventDateDTO.EventStartDate = $("#dpStartDate").val();
            eventDateDTO.EventEndDate = $("#dpEndDate").val();
            eventDateDTO.SubscribeStartDate = $("#dpSubsEndDate").val();
            eventDateDTO.SubscribeEndDate = $("#dpSubsEndDate").val();
            eventDateDTO.EventRecurringDTO = eventRecurring;

            var eventDTO = new Object();
            eventDTO.Name = $("#txtName").val();
            eventDTO.Description = $("#txtDescription").val();
            eventDTO.PersonInCharge = $("#selectTriggered").val();
            eventDTO.Room = $("#selLocation").val();
            eventDTO.CreatedDate = new Date();
            eventDTO.Private = $("#selPrivate").val();
            eventDTO.Active = true;
            eventDTO.EventDateDTO = eventDateDTO;

            $.ajax({
                type: "post",
                url: "/queue/InsertEvent",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(eventDTO),
                datatype: "json"
            });

            Admin.clearInputs();
            Admin.reloadGrid();
        });

        $("#btnUpdate").click(function () {
            var eventDateDTO = new Object();
            eventDateDTO.EventStartDate = $("#dpStartDate").val();
            eventDateDTO.EventEndDate = $("#dpEndDate").val();
            eventDateDTO.SubscribeStartDate = $("#dpSubsEndDate").val();
            eventDateDTO.SubscribeEndDate = $("#dpSubsEndDate").val();

            var EventDTO = new Object();
            EventDTO.Id = $("#txtId").val();
            EventDTO.Name = $("#txtName").val();
            EventDTO.Description = $("#txtDescription").val();
            EventDTO.PersonInCharge = $("#selectTriggered").val();
            EventDTO.Room = $("#selLocation").val();
            EventDTO.CreatedDate = new Date();
            EventDTO.Private = $("#selPrivate").val();
            EventDTO.Active = true;
            EventDTO.EventDateDTO = eventDateDTO;

            $.ajax({
                type: "POST",
                url: "/queue/UpdateEvent",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(EventDTO),
                dataType: "json"
            });

            Admin.clearInputs();
            Admin.reloadGrid();

            $("#btnUpdate").hide();
            $("#btnDelete").hide();
            $("#btnAdd").show();
        });

        $("#btnDelete").click(function () {
            var id = $("#txtId").val();

            $.ajax({
                type: "POST",
                url: "/queue/DeleteEvent",
                data: {Id: id}
            });

            Admin.clearInputs();
            Admin.reloadGrid();

            $("#btnUpdate").hide();
            $("#btnDelete").hide();
            $("#btnAdd").show();
        });
    },
    convertJsonDate: function (dateValue) {
        var a = dateValue;
        var retrieveDate = new Date(parseInt(a.substr(6)));

        var month = retrieveDate.getMonth() + 1;
        var day = retrieveDate.getDate();
        var year = retrieveDate.getFullYear();
        var date = day + "/" + month + "/" + year;
        return date;
    }
};

$(document).ready(function () {
    Admin.init();
})

